package com.beidu.lottery.call.controller;

import com.beidu.lottery.call.entity.PortalActivityRule;
import com.beidu.lottery.call.entity.dto.ActivityVoteDTO;
import com.beidu.lottery.call.entity.query.PortalGroupOptionQuery;
import com.beidu.lottery.call.entity.query.PortalOptionQuery;
import com.beidu.lottery.call.entity.vo.ActivityVoteGroupVO;
import com.beidu.lottery.call.entity.vo.PortalOptionVO;
import com.beidu.lottery.call.result.Res;
import com.beidu.lottery.call.service.portal.ActivityVoteService;
import com.beidu.lottery.call.service.portal.validator.impl.VoteValidator;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 投票活动 前端控制器
 * </p>
 *
 * @author 北渡
 * @since 2023-08-23
 */
@Slf4j
@RestController
@RequestMapping("/activity/vote")
public class ActivityVoteController {

    @Resource
    private VoteValidator voteValidator;

    @Resource
    private ActivityVoteService activityVoteService;

    /**
     * 投票数据提交
     *
     * @author 北渡
     */
    @PostMapping("/submit")
    public Res<String> voteSubmit(@Validated ActivityVoteDTO voteDTO) {
        // 投票规则校验
        PortalActivityRule rule = voteValidator.validate(voteDTO.getActivityId());
        // 投票数据预提交
        return Res.success(activityVoteService.votePreSubmit(voteDTO, rule));
    }

    /**
     * 查询投票组
     *
     * @param id 活动ID
     * @author 北渡
     */
    @GetMapping("/group/{id}")
    public Res<List<ActivityVoteGroupVO>> queryVoteGroup(@PathVariable Integer id) {
        voteValidator.statusValidate(id);// 上下架校验
        return Res.success(activityVoteService.queryVoteGroupCache(id));
    }

    /**
     * 分页搜索选项（关键字搜索）
     *
     * @author 北渡
     */
    @PostMapping("/pageVoteOption")
    public Res<PageInfo<PortalOptionVO>> pageVoteOption(
            @Validated({PortalGroupOptionQuery.VoteSearch.class}) PortalGroupOptionQuery param) {
        voteValidator.statusValidate(param.getActivityId());// 上下架校验
        return Res.success(activityVoteService.pageVoteOptionCache(param));
    }

    /**
     * 分页查询投票组选项
     *
     * @author 北渡
     */
    @PostMapping("/pageVoteGroupOption")
    public Res<PageInfo<PortalOptionVO>> pageVoteGroupOption(@Validated PortalGroupOptionQuery param) {
        voteValidator.statusValidate(param.getActivityId());// 上下架校验
        return Res.success(activityVoteService.pageVoteOptionCache(param));
    }

    /**
     * 查询单个投票选项（投票详情）
     *
     * @author 北渡
     */
    @PostMapping("/detail")
    public Res<PortalOptionVO> queryVoteOption(@Validated PortalOptionQuery param) {
        voteValidator.statusValidate(param.getActivityId());// 上下架校验
        return Res.success(activityVoteService.queryVoteOptionCache(param));
    }

}
